package com.rc.saas.tenant.service.hlht;

import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.mapper.hlht.HlhtTenantOpenPriceMapper;
import com.rc.saas.tenant.model.elec.ElecStationPrice;
import com.rc.saas.tenant.model.elec.ElecStationPriceExample;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpen;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpenPrice;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpenPriceExample;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpenStation;
import com.rc.saas.tenant.service.elec.ElecStationPriceService;
import com.rc.saas.tenant.vo.hlht.HlhtTenantOpenStationPriceVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

@Service
public class HlhtTenantOpenPriceServiceImpl implements HlhtTenantOpenPriceService {
    private static Logger logger = LoggerFactory.getLogger(HlhtTenantOpenPriceServiceImpl.class);

    @Autowired
    private HlhtTenantOpenPriceMapper hlhtTenantOpenPriceMapper;

    @Autowired
    private ElecStationPriceService elecStationPriceService;

    @Override
    public HlhtTenantOpenPriceMapper getMapper() {
        return hlhtTenantOpenPriceMapper;
    }

    /**
     * 与关闭互联站点时，删除互联的时间段价格
     * @param hlhtTenantOpenStation
     * @return
     */
    public TResponse deleteHlhtTenantOpenPriceByOpenStation(HlhtTenantOpenStation hlhtTenantOpenStation){
        String hlhtOpenType = hlhtTenantOpenStation.getHlhtOpenType();
        String tenantCode = hlhtTenantOpenStation.getTenantCode();
        String hlhtTenantCode = hlhtTenantOpenStation.getHlhtTenantCode();
        String hlhtOperatorId = hlhtTenantOpenStation.getHlhtOperatorId();

        HlhtTenantOpenPriceExample example = new HlhtTenantOpenPriceExample();
        HlhtTenantOpenPriceExample.Criteria criteria = example.createCriteria().andTenantCodeEqualTo(tenantCode);
        if ("0".equals(hlhtOpenType)){
            criteria.andHlhtTenantCodeEqualTo(hlhtTenantCode);
        }else {
            criteria.andHlhtOperatorIdEqualTo(hlhtOperatorId);
        }
        criteria.andElecStationIdEqualTo(hlhtTenantOpenStation.getElecStationId());
        try {
            this.deleteByExample(example);
        }catch (Exception e){
            logger.error("删除失败", e);
            return TResponse.FAIL("删除失败");
        }
        return TResponse.SUCCESS();
    }

    @Override
    public List<HlhtTenantOpenPrice> selectByBatch(List<Long> elecStationIdList, HlhtTenantOpen hlhtTenantOpen) {
        return hlhtTenantOpenPriceMapper.selectByBatch(elecStationIdList, hlhtTenantOpen);
    }

    @Override
    public void updateOperateAdjustValueByBatch(List<HlhtTenantOpenPrice> hlhtTenantOpenPriceList, BigDecimal adjustValue) {
        hlhtTenantOpenPriceMapper.updateOperateAdjustValueByBatch(hlhtTenantOpenPriceList, adjustValue);
    }

    @Override
    public void updateHlhtTenantOpenPrice(List<HlhtTenantOpenStationPriceVo> hlhtTenantOpenStationPriceVoList) {
        hlhtTenantOpenPriceMapper.updateHlhtTenantOpenPrice(hlhtTenantOpenStationPriceVoList);
    }
}
